package service;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import properties.MyAppConstantes;

import android.os.AsyncTask;
import controller.MainActivity;

/**
 * @author ALENGUYE on sup infos user avc son id
 */
public class DeleteUserAsyncTask extends AsyncTask<Object, Void, String> {
	private MainActivity Context = null;
	private String idUser;
	private String result;
	private String hash;
	private String timestamp;
	private String status;
	private String errorCode;

	public DeleteUserAsyncTask(MainActivity context, String idUser) {
		super();
		Context = context;
		this.idUser = idUser;
	}

	@Override
	protected void onPreExecute() {
	}

	@Override
	protected String doInBackground(Object... params) {

		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("http://" + MyAppConstantes.WsIpServer
				+ ":8080/ab_service_mgr/api/mobile/delete/" + idUser);

		ResponseHandler<String> responseHandler = new BasicResponseHandler();

		httpPost.setHeader("content-type", "application/json");

		try {
			timestamp = System.currentTimeMillis() + "";
			hash = Sha1Utils.sha(MyAppConstantes.wsSecret + timestamp);

			JSONObject json = new JSONObject();
			json.put("timestamp", timestamp);
			json.put("hash", hash);
			json.put("wsCallerId", MyAppConstantes.wsCallerId);

			StringEntity entity = new StringEntity(json.toString());
			entity.setChunked(false);

			httpPost.setEntity(entity);
			httpPost.setHeader("Content-Type", "application/json");
			// entity.setContentEncoding("application/json");

			result = client.execute(httpPost, responseHandler);
			JSONObject jsonResp = new JSONObject(result);
			status = jsonResp.getString("status");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;

	}

	protected void onPostExecute(String status) {
		if ((status != null && status.equals("false"))
				|| (errorCode != null && errorCode.equals("200"))) {
			Context.showDialog(MyAppConstantes.INTEGER_8);
		} else {
			Context.showDialog(MyAppConstantes.INTEGER_7);
		}
	}

}
